package de.codi.structural;

import java.util.HashSet;
import java.util.List;

import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLObjectProperty;

import com.googlecode.rockit.app.result.RockItResult;

import de.codi.exception.CodiException;
import de.codi.read.Storage;
import de.unima.alcomox.exceptions.CorrespondenceException;
import de.unima.alcomox.mapping.Correspondence;
import de.unima.alcomox.mapping.Mapping;
import de.unima.alcomox.mapping.SemanticRelation;


public class AlignmentGenerator
{
    public Mapping convertToMapping(List<RockItResult> rockItResult) throws CodiException
    {
        Mapping mapping = new Mapping();

        HashSet<OWLClass> classes1 = Storage.source.classes;
        HashSet<OWLClass> classes2 = Storage.target.classes;

        HashSet<OWLObjectProperty> objectProperties1 = Storage.source.objectProperties;
        HashSet<OWLObjectProperty> objectProperties2 = Storage.target.objectProperties;

        HashSet<OWLDataProperty> dataProperties1 = Storage.source.dataProperties;
        HashSet<OWLDataProperty> dataProperties2 = Storage.target.dataProperties;

        for(RockItResult result : rockItResult) {
            String[] resultArray = new String[] { result.getPredicate(), result.getObjects().get(0), result.getObjects().get(1) };
            Correspondence c = null;
            if(resultArray[0].equals(StructuralConstants.CMAP)) {
                c = toCorrespondence(classes1, resultArray[1], classes2, resultArray[2]);
            } else if(resultArray[0].equals(StructuralConstants.PMAP)) {
                c = toCorrespondence(objectProperties1, resultArray[1], objectProperties2, resultArray[2]);
                if(c == null) {
                    c = toCorrespondence(dataProperties1, resultArray[1], dataProperties2, resultArray[2]);
                }
            }

            if(c != null)
                mapping.push(c);
        }

        return mapping;

    }


    private String returnFullName(String suffix, HashSet<? extends OWLEntity> entities)
    {
        for(OWLEntity e : entities) {
            if(e.toString().equals(suffix)) { return e.getIRI().toURI().toString(); }
        }
        return null;
    }


    private Correspondence toCorrespondence(HashSet<? extends OWLEntity> entity1, String suffix1, HashSet<? extends OWLEntity> entity2, String suffix2) throws CodiException
    {
        Correspondence c = null;

        String sourceEntity = returnFullName(suffix1, entity1);
        String targetEntity = returnFullName(suffix2, entity2);
        SemanticRelation equiv = null;
        try {
            equiv = new SemanticRelation(SemanticRelation.EQUIV);
            if(sourceEntity != null && targetEntity != null) {
                c = new Correspondence(sourceEntity, targetEntity, equiv, 1);
            }

            return c;

        } catch(CorrespondenceException e) {
            throw new CodiException("failure in converting matrix cell to correspondence", e);
        }
    }
}
